<template>
  <el-dialog
    title="请选择商品"
    width="800px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @closed="handleDialogClosed"
    @open="handleDialogOpen"
  >
    <app-card>
      <template #header>
        <el-form
          ref="form"
          :model="listParams"
          :disabled="listLoading"
          label-width="80px"
          inline
          @submit.prevent="handleQuery"
        >
          <el-form-item label="商品分类">
            <el-select
              v-model="listParams.cate_id"
              placeholder="请选择"
              clearable
              @change="loadList"
            >
              <el-option
                label="全部"
                :value="0"
              />
              <el-option
                v-for="item in productCates"
                :key="item.id"
                :label="`${item.html}${item.cate_name}`"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input
              v-model="listParams.store_name"
              placeholder="请输入商品名称关键字"
              clearable
              style="width: 300px;"
            >
              <template #append>
                <el-button
                  icon="el-icon-search"
                  @click="loadList"
                />
              </template>
            </el-input>
          </el-form-item>
        </el-form>
      </template>
      <el-table
        :data="list"
        v-loading="listLoading"
        style="width: 100%"
      >
        <el-table-column
          label="#"
          width="55"
        >
          <template #default="scope">
            <el-radio
              label=" "
              @change="props.selected(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="id"
          width="80"
          label="商品ID"
        />
        <el-table-column
          prop="image"
          width="100"
          label="商品图片"
        >
          <template #default="scope">
            <el-image
              style="width: 38x; height: 38px"
              :src="scope.row.image"
              :preview-src-list="[scope.row.image]"
            >
              <template #error>
                <div class="image-slot">
                  <i
                    class="el-icon-picture"
                    style="font-size: 23px; margin: 5px; color: rgb(219, 219, 219);"
                  />
                </div>
              </template>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="store_name"
          label="商品名称"
        />
        <el-table-column
          prop="cate_name"
          label="商品分类"
        />
      </el-table>
      <app-pagination
        v-model:page="listParams.page"
        v-model:limit="listParams.limit"
        :list-count="listCount"
        :load-list="loadList"
        :disabled="listLoading"
      />
    </app-card>
  </el-dialog>
</template>
<script lang="ts" setup>
import type { PropType } from 'vue'
import { reactive, ref } from 'vue'
import * as productApi from '@/api/product'
import type { Product, ProductListParams, ProductCategory } from '@/api/types/product'

const productCates = ref<ProductCategory[]>([])
const list = ref<Product[]>([])
const listCount = ref(0)
const listLoading = ref(false)
const listParams = reactive<ProductListParams>({
  page: 1,
  limit: 10,
  cate_id: 0,
  type: 0,
  store_name: '',
  sales: 'normal'
})

const props = defineProps({
  selected: {
    type: Function as PropType<(product:Product) => Promise<void>>,
    default: () => Promise.resolve()
  }
})
interface EmitsType {
  (e: 'cancel'): void
}
const emit = defineEmits<EmitsType>()
const handleDialogClosed = () => {
  emit('cancel')
}
const handleDialogOpen = () => {
  loadList()
  loadProductCates()
}

const loadList = async () => {
  listLoading.value = true
  const data = await productApi.getProducts(listParams).finally(() => {
    listLoading.value = false
  })
  data.list.forEach(item => {
    item.statusLoading = false
  })
  list.value = data.list
  listCount.value = data.count
}

const loadProductCates = async () => {
  const data = await productApi.getCategoryTree(1)
  productCates.value = data
}

const handleQuery = () => {
  listParams.page = 1
  loadList()
}

</script>
<style lang="scss" scoped>
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.demo-table-expand {
  font-size: 0;
  :deep(label) {
    width: 90px;
    color: #99a9bf;
  }
  :deep(.el-form-item) {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
}

.image-slot {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
